/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package z.h.w.jar.kit.method;

import java.net.NetworkInterface;
import java.net.SocketException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import z.h.w.jar.data.time.Attr;
import z.h.w.jar.data.time.Time;
import z.h.w.jar.kit.clazz.security.GyrateCode;

/**
 *
 * @author EnzoZhong
 */
public class ED {

      final private static char MD5_HEX[] =
                                  To.characterArray2CharArray ( To.string2CharacterArray ( "0123456789abcdef" ) );

      /**
       *
       * @param source
       * @return
       */
      public static synchronized String MD5 ( String source ) {
            return MD ( source.getBytes () , "MD5" );
      }

      public static synchronized String safe ( String source ) {
            GyrateCode gyrateCode = GyrateCode.born ();
            Time time = Time.born ();
            String date = time.tell ( Attr.YEAR ) + time.tell ( Attr.MONTH ) + time.tell ( Attr.DAY );
            String code = "";
            code += string2InreversiblePassword ( source );
            code += string2InreversiblePassword ( date );
            code = gyrateCode.加密 ( code , 1 , true );
            return code;
      }

      /**
       *
       * @param source
       *
       * @return
       */
      public static synchronized String SHA ( String source ) {
            return MD ( source.getBytes () , "SHA" );
      }

      /**
       *
       * @param array
       * @param type
       *
       * @return
       */
      public static synchronized String MD ( byte[] array , String type ) {
            MessageDigest messageDigest = null;
            try {
                  messageDigest = java.security.MessageDigest.getInstance ( type );
            } catch ( NoSuchAlgorithmException ex ) {
                  System.out.println ( ex );
            }
            messageDigest.update ( array );
            // MD5 的计算结果是一个 128 位的长整数， 
            return byte2String ( messageDigest.digest () );
      }

      private static String byte2String ( byte tmp[] ) {
            // 用字节表示就是 16 个字节
            char str[] = new char[ 16 * 2 ];
            // 所以表示成 16 进制需要 32 个字符
            int count = 0;
            for ( int i = 0 ; i < 16 ; i ++ ) {
                  // 转换成 16 进制字符的转换
                  byte byte0 = tmp[i];
                  // 取字节中高 4 位的数字转换, 
                  str[count ++] = MD5_HEX[byte0 >>> 4 & 0xf];
                  // >>> 为逻辑右移，将符号位一起右移
                  // 取字节中低 4 位的数字转换
                  str[count ++] = MD5_HEX[byte0 & 0xf];
            }
            return new String ( str );
      }

      /**
       * ezrf使用中
       */
      public static synchronized String string2InreversiblePassword ( String plaintext ) {
            GyrateCode gyrateCode = GyrateCode.born ();
            plaintext = MD5 ( plaintext );
            plaintext = gyrateCode.加密 ( plaintext , 1 , true );
            plaintext = SHA ( plaintext );
            plaintext = gyrateCode.加密 ( plaintext , 1 , true );
            return plaintext;
      }

      /**
       * 先把密码转成base64，然后再通过md5加密，这样的好处是，防止别人得知md5密码后，直接通过查表
       * 的方法来得知原密码，该方法是多个项目的密码转换器，所以没有特别变动，绝对不能更改，最多改名
       *
       * @param plaintext
       *
       * @return
       */
      public static synchronized String toInreversiblePassword ( String plaintext ) {
            String md5 = MD5 ( plaintext );
            String sha = SHA ( md5 );
            return sha;
      }

      public static String getMACAddress ( NetworkInterface ni ) {
            //下面代码是把mac地址拼装成String
            StringBuffer sb = new StringBuffer ();
            try {
                  byte[] mac = ni.getHardwareAddress ();
                  if ( mac == null ) {
                        return "NaN";
                  }
                  Integer len = mac.length;
                  for ( int i = 0 ; i < len ; i ++ ) {
                        if ( i != 0 ) {
                              sb.append ( "-" );
                        }
                        //mac[i] & 0xFF 是为了把byte转化为正整数
                        String s = Integer.toHexString ( mac[i] & 0xFF );
                        sb.append ( s.length () == 1 ? 0 + s : s );
                  }
                  //把字符串所有小写字母改为大写成为正规的mac地址并返回
                  return sb.toString ().toUpperCase ();
            } catch ( SocketException ex ) {
                  return "NaN";
            }
      }

}
